package com.my.mall.manage.goods.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.my.mall.common.Base;
import com.my.mall.common.R;
import com.my.mall.manage.goods.dto.image.GoodsImageManageDTO;
import com.my.mall.manage.goods.dto.info.GoodsInfoManageDTO;
import com.my.mall.manage.goods.service.GoodsManageService;
import com.my.mall.manage.goods.vo.category.GoodsCategoryVo;
import com.my.mall.manage.goods.vo.image.GoodsImageManageVo;
import com.my.mall.manage.goods.vo.info.GoodsInfoVo;
import com.my.mall.manage.goods.vo.sold.GoodsSoldManageVo;
import com.my.mall.manage.imageInfo.service.ImageInfoManageService;

@Controller
@RequestMapping("manage/goods")
public class GoodsManageController extends Base {

	@Autowired
	GoodsManageService goodsManageService;

	@Autowired
	ImageInfoManageService imageInfoManageService;

	/**
	 * 查询商品所有类别
	 * 
	 * @return
	 */
	@RequestMapping("selectCategoryList")
	@ResponseBody
	public List<Map<String, Object>> selectCategoryList() {
		GoodsCategoryVo pv = new GoodsCategoryVo();
		pv.setTopId(0);
		List<Map<String, Object>> list = goodsManageService.selectCategoryList(pv, null, null);
		return list;
	}

	/**
	 * 保存和修改商品类别信息
	 * 
	 * @param id
	 * @param categoryName
	 * @param topId
	 * @return
	 */
	@RequestMapping("saveCategory")
	@ResponseBody
	public R saveCategory(int id, String categoryName, int topId, int imgId) {
		int result = 0;
		if (id == 0)
			result = goodsManageService.saveCategory(categoryName, topId, imgId);
		else
			result = goodsManageService.updateCategory(id, categoryName, topId, imgId);
		if (result == 1)
			return R.ok();
		else
			return R.error("系统故障！");
	}

	/**
	 * 根据商品类别id 查询该分类信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("getCategory/{id}")
	@ResponseBody
	public R getCategory(@PathVariable("id") int id) {
		GoodsCategoryVo categoryDate = goodsManageService.getCategory(id);
		return R.ok().put("categoryDate", categoryDate);
	}

	/**
	 * 根据商品类别ID删除该商品类别
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("delCategory/{id}")
	@ResponseBody
	public R delCategory(@PathVariable("id") int id) {
		int result = goodsManageService.delCategory(id);
		if (result == 1)
			return R.ok();
		else if (result == 2)
			return R.error(2, "该分类下有子分类不能直接删除,请删除该分类下的所有子分类后再删除该分类！");
		else
			return R.error("系统故障");
	}

	/**
	 * 查询商品详细信息
	 * 
	 * @param iDisplayStart
	 * @param iDisplayLength
	 * @return
	 */
	@RequestMapping("selectInfoList")
	@ResponseBody
	public Map<String, Object> selectInfoList(int iDisplayStart, int iDisplayLength) {
		List<GoodsInfoVo> list = goodsManageService.selectInfoList(iDisplayStart, iDisplayLength);
		int size = goodsManageService.selectInfoCount();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("iTotalRecords", size);
		map.put("iTotalDisplayRecords", size);
		map.put("aaData", list);
		return map;
	}

	/**
	 * 保存和修改商品详细信息
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("saveInfo")
	@ResponseBody
	public R saveInfo(GoodsInfoManageDTO info) {
		int result = 0;
		if (info.getId() == 0)
			result = goodsManageService.saveInfo(info);
		else
			result = goodsManageService.updateInfo(info);

		if (result == 1)
			return R.ok();
		else
			return R.error("系统故障！");
	}

	/**
	 * 根据商品id 查询该商品信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("getInfo/{id}")
	@ResponseBody
	public R getInfo(@PathVariable("id") int id) {
		GoodsInfoVo infoDate = goodsManageService.getInfo(id);
		return R.ok().put("infoDate", infoDate);
	}

	/**
	 * 根据商品ID删除该商品
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("delInfo/{id}")
	@ResponseBody
	public R delInfo(@PathVariable("id") int id) {
		int result = goodsManageService.delInfo(id);
		if (result == 1) {
			// imageInfoManageService.delImgByGoodsId(id);
			return R.ok();
		} else
			return R.error("系统故障");
	}

	/**
	 * 查询商品是否上架信息
	 * 
	 * @param iDisplayStart
	 * @param iDisplayLength
	 * @return
	 */
	@RequestMapping("selectShelvesList")
	@ResponseBody
	public Map<String, Object> selectShelvesList(int iDisplayStart, int iDisplayLength) {
		List<GoodsInfoVo> list = goodsManageService.selectInfoList(iDisplayStart, iDisplayLength);
		int size = goodsManageService.selectInfoCount();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("iTotalRecords", size);
		map.put("iTotalDisplayRecords", size);
		map.put("aaData", list);
		return map;
	}

	/**
	 * 修改商品上架信息
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("updateShelves")
	@ResponseBody
	public R updateShelves(GoodsInfoManageDTO info) {
		int result = 0;
		result = goodsManageService.updateShelves(info);
		if (result == 1)
			return R.ok();
		else
			return R.error("系统故障！");
	}

	/**
	 * 查询已卖出商品详细信息
	 * 
	 * @param iDisplayStart
	 * @param iDisplayLength
	 * @return
	 */
	@RequestMapping("selectSoldInfoList")
	@ResponseBody
	public Map<String, Object> selectSoldInfoList(int iDisplayStart, int iDisplayLength) {
		List<GoodsSoldManageVo> list = goodsManageService.selectSoldInfoList(iDisplayStart, iDisplayLength);
		int size = goodsManageService.selectSoldInfoCount();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("iTotalRecords", size);
		map.put("iTotalDisplayRecords", size);
		map.put("aaData", list);
		return map;
	}

	/**
	 * 添加商品的预览图
	 * 
	 * @param imgInfo
	 * @return
	 */
	@RequestMapping("saveImageInfo")
	@ResponseBody
	public R saveImageInfo(GoodsImageManageDTO imgInfo) {
		int result = goodsManageService.updateImageInfo(imgInfo);
		if (result > 0)
			return R.ok();
		else
			return R.error("系统故障！");
	}

	/**
	 * 根据商品ID取出该商品的所有图片信息
	 * 
	 * @param id商品ID
	 * @return
	 */
	@RequestMapping("selectImageInfoList/{id}")
	@ResponseBody
	public R selectImageInfoList(@PathVariable("id") int id) {
		GoodsImageManageVo imageInfoDate1 = new GoodsImageManageVo();
		GoodsImageManageVo imageInfoDate2 = new GoodsImageManageVo();
		List<GoodsImageManageVo> imageInfoDateList = new ArrayList<GoodsImageManageVo>();
		List<GoodsImageManageVo> imgInfoDates = goodsManageService.selectImageInfoList(id);
		for (GoodsImageManageVo imageInfoDate : imgInfoDates) {
			if (imageInfoDate.getImgType() == 0) {
				imageInfoDate1 = imageInfoDate;
			} else if (imageInfoDate.getImgType() == 1) {
				imageInfoDate2 = imageInfoDate;
			} else if (imageInfoDate.getImgType() == 2) {
				imageInfoDateList.add(imageInfoDate);
			}
		}
		if (imgInfoDates.size() > 0) {
			return R.ok().put("imageInfoDate1", imageInfoDate1).put("imageInfoDate2", imageInfoDate2)
					.put("imageInfoDateList", imageInfoDateList);
		} else {
			return R.error(1, "没有该商品的图片信息");
		}

	}

	/**
	 * 根据图片Id删除该商品对应的图片信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("delImg/{id}")
	@ResponseBody
	public R delImg(@PathVariable("id") int id) {
		int result = goodsManageService.delImg(id);
		if (result == 1) {
			return R.ok();
		} else
			return R.error("系统故障");
	}

}